<template>
  <!-- 登录页面 -->
  <div class="login" :class="{ hasbg: showBg }">
    <!--顶部操作栏 -->
    <div class="login-top">
      <!-- <span class="menu-icon config" :title="$t('login.cfgt')" @click="showzhuce">
        <svg-icon icon-class="config" />
        <span class="text">{{ $t('login.config') }}</span>
      </span> -->
      <span class="menu-icon" :title="$t('menu.mini')" @click="handleWinMinify">
        <svg-icon icon-class="minify" />
      </span>
      <span class="menu-icon close" :title="$t('menu.close')" @click="handleWinClose">
        <svg-icon icon-class="close" />
      </span>
    </div>
    <div class="login-box">
      <div class="logn-img">
        <!--        <img src="../../assets/img/loginbg.jpg"/>-->
      </div>
      <!-- 账号密码输入 -->
      <!-- <el-form :model="loginForm" :rules="rules" ref="loginForm" class="login-form">
        <el-form-item class="login-form-item" prop="username">
          <el-input placeholder="请输入账号" type="text" clearable v-model="loginForm.username">
            <span slot="prefix" class="input-icon">
              <svg-icon slot="prefix" icon-class="user" />
            </span>
          </el-input>
        </el-form-item>
        <el-form-item class="login-form-item" prop="password">
          <el-input placeholder="请输入密码" type="password" clearable v-model="loginForm.password">
            <span slot="prefix" class="input-icon">
              <svg-icon icon-class="lock" />
            </span>
          </el-input>
          <div class="version">版本号：{{ version }}</div>
        </el-form-item>
        <el-form-item class="login-form-item check-box">
          <el-checkbox :value="remName" @change="v => checkboxSwitch('1', v)">{{ $t('login.run') }}</el-checkbox>
        </el-form-item>
        <el-form-item class="login-form-item check-box">
          <el-checkbox :value="remPwd" @change="v => checkboxSwitch('2', v)">{{ $t('login.rpwd') }}</el-checkbox>
        </el-form-item>
        <el-form-item class="login-form-item">
          <button class="submit" @click="loginSystem">{{ $t('login.lgbtn') }}</button>
        </el-form-item>
      </el-form> -->
      <transition name="el-fade-in">
        <network v-show="showLine" @clickclose="closeNetwork" />
      </transition>
    </div>
  </div>
</template>

<script>
const shell = require('electron').shell
const { ipcRenderer } = require("electron");
import Network from "./Network";
import { setTimeout } from "timers";
import config from "../../../../package.json"

export default {
  name: "Login",
  components: {
    Network
  },
  data() {
    return {
      version: '',
      loginForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      },
      showLine: false,
      showBg: true
    };
  },
  computed: {
    // userinfo() {
    //   return this.$store.getters.userinfo;
    // },
    remName() {
      return this.$store.getters.remname;
    }, // 记住用户名
    remPwd() {
      return this.$store.getters.rempwd;
    } // 记住密码
  },
  beforeMount() {
    // 如果是记住用户名和密码，将其用户名和密码填入
    let fromData = this.$db
      .read()
      .get("users.loginFrom")
      .value();
    if (this.remName) {
      this.loginForm.username = fromData.username;
    }
    if (this.remPwd) {
      this.loginForm.password = fromData.password;
    }
  },
  mounted() {
    this.version = config.version
    this.$message({
      showClose: true,
      message: '登录中...',
      type: 'success',
      duration: 2100
    });
    setTimeout(() => {
      ipcRenderer.send("win-msg", "reset-wh"); // 最大化
      this.$router.push({ path: "/home" });
      let { username, password } = this.loginForm;
      // ipcRenderer.send("win-msg", "reset-wh"); // 当前本地配置不是最大化
      // 将账号密码存入本地，每次退出时清除本地的账号密码信息
      this.$db
        .read()
        .set("users.loginFrom", {
          username,
          password
        })
        .write(); // 写入用户名
    }, 2000)
  },
  methods: {
    showzhuce() {
      const inURL = `https://zhuce.tianhengqihuo.com`
      console.log(inURL);
      shell.openExternal(inURL)
    },
    loginSystem() {
      let that = this;
      let isZoom = this.$db
        .read()
        .get("users.zoom")
        .value();
      // 登录系统
      this.$refs["loginForm"].validate(valid => {
        if (valid) {
          let { username, password } = this.loginForm;
          // 登录直接跳转主界面行情
          that.$router.push({ path: "/home" });
          if (isZoom) {
            ipcRenderer.send("win-msg", "reset"); // 重设配置
            ipcRenderer.send("win-msg", "zoom"); // 最大化
          } else {
            ipcRenderer.send("win-msg", "reset-wh"); // 当前本地配置不是最大化
          }

          // 将账号密码存入本地，每次退出时清除本地的账号密码信息
          that.$db
            .read()
            .set("users.loginFrom", {
              username,
              password
            })
            .write(); // 写入用户名
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    checkboxSwitch(type, val) {
      console.log(type, val);
      // 写入本地数据库配置是否记住用户名
      if (type === "1") {
        this.$db
          .read()
          .set("users.remName", val)
          .write();
      }

      // 写入本地数据库配置是否记住密码
      if (type === "2") {
        this.$db
          .read()
          .set("users.remPwd", val)
          .write();
      }

      this.$store.dispatch("user/keepInMind", { type, val });
    },
    handleWinClose() {
      // 退出应用
      ipcRenderer.send("win-msg", "close");
    },
    handleWinMinify() {
      // 最小化应用
      ipcRenderer.send("win-msg", "minify");
    },
    closeNetwork() {
      this.showLine = false;
    }
  }
};
</script>

<style lang="scss">
.version {
  text-align: right;
  margin-top: 7px;
  color: #999999;
  font-size: 12px;
}

.login {
  position: relative;
  width: 100vw;
  height: 100vh;
  background: #fff;
  -webkit-app-region: drag;

  &.hasbg {
    background: url("../../assets/img/loginbg.jpg") no-repeat top center;
    background-size: 100%;

    .login-box {
      display: flex;
      height: 100vh;

      .logn-img {
        display: flex;
        align-items: center;
        justify-content: center;
        flex: 2;

        img {
          width: 150px;
          height: auto;
        }
      }
    }
  }

  &-top {
    position: absolute;
    top: 0;
    right: 8px;
    display: flex;
    overflow: hidden;
    border-radius: 0 0 4px 4px;
    -webkit-app-region: no-drag;

    .menu-icon {
      padding: 3px 11px;
      vertical-align: middle;
      color: #ffffff;
      background-color: #9e9e9e;
      cursor: pointer;

      .text {
        font-size: 12px;
      }

      &.config {
        background-color: #7a7a7a;
        margin-right: 4px;
        padding: 3px 12px;

        .svg-icon {
          font-size: 14px;
        }
      }

      &.close {
        background-color: rgb(191, 17, 29);
      }
    }
  }

  &-form {
    flex: 1;
    margin-top: 106px;

    &-item {
      -webkit-app-region: no-drag;
      width: 170px;
      margin-bottom: 14px;
      overflow: hidden;

      &.el-form-item {
        &.is-success {

          .el-input__inner,
          .el-textarea__inner {
            border-color: #dcdfe6;
          }
        }

        .el-form-item__content {
          line-height: 1;
        }
      }

      .el-input {
        .el-input__inner {
          height: 32px;
          line-height: 32px;
        }

        .el-input__prefix {
          top: 8px;

          .input-icon {
            font-size: 15px;
            color: #cc9936;
          }
        }
      }

      &.check-box {
        margin-bottom: 7px;
        padding-left: 12px;

        .el-checkbox .el-checkbox__label {
          font-size: 12px;
        }
      }

      .submit {
        margin-top: 30px;
        width: 166px;
        height: 44px;
        border: none;
        outline: none;
        font-size: 14px;
        font-weight: 700;
        letter-spacing: 6px;
        color: #ffffff;
        background-color: #cb9f48;
        cursor: pointer;
      }
    }
  }
}
</style>
